# -*- coding: utf-8 -*-

# @Author : guoxin

# @Time : 2024/4/1 18:48

# @QQ :363673234
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from common.core import get_local_webdriver_path

from common.tools import get_project_path, sep


class DriverConfig:
    def driver_config(self):
        """
        浏览器驱动
        :return:
        """
        options = webdriver.ChromeOptions()
        # 设置窗口大小，设置为1920*1080
        options.add_argument("--window-size=1920,1080")
        # 去除"chrome正受到自动测试软件的控制"的提示
        options.add_experimental_option("excludeSwitches", ["enable-automation"])
        # 解决selenium无法访问https的问题
        options.add_argument("--ignore-certificate-errors")
        # 允许忽略localhost上的TLS/SSL错误
        options.add_argument("--allow-insecure-localhost")
        # 设置为无痕模式
        options.add_argument("--incognito")
        # 设置为无头模式
        # options.add_argument("--headless")
        # 解决卡顿
        options.add_argument("--disable-gpu")
        options.add_argument("--no-sandbox")
        options.add_argument("--disable-dev-shm-usage")
        options.add_experimental_option("detach", True)
        # options.page_load_strategy = 'eager'
        options.page_load_strategy = 'none'
        # options.add_argument('blink-settings=imagesEnabled=false')
        # No_Image_loading = {"profile.managed_default_content_settings.images": 2}
        # options.add_experimental_option("prefs", No_Image_loading)
        # 115以下chrome浏览器采用此方法自动跟新浏览器对应的驱动
        # driver = webdriver.Chrome(ChromeDriverManager(url="https://registry.npmmirror.com/-/binary/chromedriver",
        #                                               latest_release_url="https://registry.npmmirror.com/-/binary/chromedriver/LATEST_RELEASE",
        #                                               cache_valid_range=365).install(),options=options)
        # 下载好的对应chrome浏览器驱动采用此方法就行
        driver = webdriver.Chrome(
            executable_path=get_project_path() + sep(["driver_files", "chromedriver"], add_sep_before=True),
            options=options)
        #115版本后采用此方法自动匹配对应的驱动
        # driver = webdriver.Chrome(executable_path=get_local_webdriver_path(),options=options)
        # # 删除所有cookies
        driver.delete_all_cookies()

        return driver
